<template>
  <div>
    <img class="headerimg" src="https://p1.meituan.net/codeman/5a2f2c551750f73c8b98eb86836514f8213171.png@750w_416h_80q" alt="">
    <cube-form
      :model="model"
      :schema="schema"
      @submit.prevent="submitHandler"
    ></cube-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      model: {
        username: "",
        password: ""
      },
      schema: {
        fields: [
          {
            type: "input",
            modelKey: "username",
            label: "用户名",
            props: {
              placeholder: "请输入用户名"
            },
            rules: {
              required: true,
              type: "string",
              min: 3,
              max: 15
            },
            trigger: "blur",
            messages: {
              required: "用户名不能为空",
              min: "用户名不能小于3个字符",
              max: "用户名不能大于15个字符"
            }
          },
          {
            type: "input",
            modelKey: "password",
            label: "密码",
            props: {
              placeholder: "请输入密码",
              type: "password",
              eye: {
                open: false
              }
            },
            rules: {
              required: true
            },
            trigger: "blur"
          },
          {
            type: "submit",
            label: "登录"
          }
        ]
      }
    };
  },
  methods: {
    //异步函数
    async submitHandler(e) {
      // e.preventDefault();
      try {
        const result=await this.$http.get('/api/login',{params:this.model})
        if (result.code=='0') {
          let token=result.token
          this.$store.commit('settoken',token)
          window.localStorage.setItem('token',token)

          //判断路由是否带参，带参就去到重定向参数地址，否则就去首页
          if (this.$route.query.redirect) {
            this.$router.replace({path:this.$route.query.redirect})
          }else{
            this.$router.replace({path:'/botnav/index'})
          }
        }else{
          alert(result.message)
        }
      } catch (error) {
        console.log(error)
      }
    }
  }
};
</script>


<style lang="stylus">
   .headerimg
       height 150px
       width  100%
</style>

